%%% Copyright 2010 Google Inc.
%%%
%%% Licensed under the Apache License, Version 2.0 (the "License");
%%% you may not use this file except in compliance with the License.
%%% You may obtain a copy of the License at
%%%
%%%     http://www.apache.org/licenses/LICENSE-2.0
%%%
%%% Unless required by applicable law or agreed to in writing, software
%%% distributed under the License is distributed on an "AS IS" BASIS,
%%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%%% See the License for the specific language governing permissions and
%%% limitations under the License.

%%% Author <jacoby@google.com> Jacoby Thwaites [http://sites/madcloud]
%%%
%%% Header file for the  view_db:, view_sup and view_fsm: modules.

%%% Idle view_fsm processes, inactive for 20 mins or more, are shutdown
%%% by a cleaner that runs every minute.
-define(VIEW_CLEANER_INTERVAL_MILLIS, (1 * 60 * 1000)).
-define(VIEW_MAX_IDLE_MILLIS, (20 * 60 * 1000)).

%%% View supervisor has view_fsm and view_db processes under it.
-define(VIEW_SUP, view_sup).

%%% Timeouts on gen_fsm:sync_send_event() calls.
-define(SYNC_EVENT_TIMEOUT, infinity).

%%% State names are function names.
%%% We use CamelCase for visual differentiation, ALL_CAPS function names
%%% are really horrid.
-define(StateIdle, state_idle).
-define(StateAttached, state_attached).

%%% State-specified events.
-define(ATTACH, attach).
-define(DETACH, detach).
-define(HANDLE, handle).
-define(NOTIFY, notify).
-define(REPLY, reply).

%% All-state events.
-define(CURRENT_STATE, current_state).
-define(GET_CLIENT_DATA, client_data).
-define(SET_CLIENT_DATA, client_data).

%%% A view can connect with a cookie, in which case only views sharing that
%%% cookie see events on the same processes. This constant is used to specify
%%% that no cookie filtering of events is required for the view.
-define(ANY_COOKIE, '$any_cookie').

%%% ---------------------------------------------------------------------------
%%% View FSM loop data name and record.
%%% Fields populated at attach time except for clientState which is
%%% an arbitrary term supplied at startup.
%%%
%%% The state data records the last time each `view_fsm' received an event
%%% from a client. This is used by `view_sup' to cleanup inactive views.
%%% ---------------------------------------------------------------------------
-define(VIEW_LOOP_DATA, view_loop_data).
-record(?VIEW_LOOP_DATA, {client = ?UNDEFINED         :: module(),
                          clientData = ?UNDEFINED     :: term(),
                          compRef = ?NULL_REF         :: ref(),
                          folderRef = ?NULL_REF       :: ref(),
                          cookie = ?UNDEFINED         :: term(),
                          opsNr = ?UNDEFINED          :: term(),
                          emitOps = dict:new()        :: dict(),
                          collectOps = dict:new()     :: dict(),
                          pendingReplies = dict:new() :: dict(),
                          lastUsed = now()            :: time()}).

%%% The pending replies field in view_loop_data holds reply records
%%% keyed by call ref.
-define(REPLY_RECORD, reply_record).
-record(?REPLY_RECORD, {callRef = ?UNDEFINED  :: term(),
                        opRef = ?NULL_REF     :: ref(),
                        fid = ?NULL_PID       :: pid(),
                        cookie = ?UNDEFINED   :: term(),
                        timestamp = now()     :: time()}).

%%% An activation record is written for each op in the folder of a view_fsm
%%%
-define(ACTIVATION, activation).
-record(?ACTIVATION, {viewFsm = ?NULL_PID  :: pid(),
                      compRef = ?NULL_REF  :: ref(),
                      cookie =  ?UNDEFINED :: term(),
                      opRef = ?NULL_REF    :: ref()}).
